package net.lahuobao.mall.controller.finance;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import net.lahuobao.enums.mall.admin.*;
import net.lahuobao.export.util.ExportFileUtil;
import net.lahuobao.mall.entity.finance.MallCarriersExpenditureAccount;
import net.lahuobao.mall.entity.finance.MallMiddleExpenditureAccount;
import net.lahuobao.mall.entity.finance.MallMiddleIncomeAccount;
import net.lahuobao.mall.entity.finance.MallOrderReportQueryModel;
import net.lahuobao.mall.entity.finance.resultmodel.MallOrderReportResultModel;
import net.lahuobao.mall.service.finance.MallReportServiceI;
import net.lahuobao.util.DateTimeExtends;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.SchemaOutputResolver;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by clark on 2016/9/2 0002.
 * 商城财务报表Controller
 */
@Controller
@RequestMapping("/mallReportController")
public class MallReportController extends BaseController {

    @Resource
    private MallReportServiceI reportService;

    @Resource
    private ExportFileUtil exportFileUtil;


    /*##########################中间商收入账户  start #########################*/

    /**
     * 中间商收入账户列表
     */
    @RequestMapping(params = "middleIncomeList")
    public ModelAndView middleIncomeList() {
        return new ModelAndView("net/lahuobao/mall/finance/middleIncomeList");
    }


    /**
     * 中间商收入账户 datagrid
     *
     * @param request
     * @param response
     */
    @RequestMapping(params = "middleIncomeDatagrid")
    public void middleIncomeDatagrid(MallMiddleIncomeAccount query, HttpServletRequest request, HttpServletResponse response,
                                     DataGrid dataGrid) {
        //输出dataggrid
        TagUtil.datagrid(response, pageDateGrid(selectIncomeListByquery(request, query), dataGrid));
    }


    @RequestMapping(params = "middleIncomeExport")
    public void middleIncomeExport(MallMiddleIncomeAccount query, HttpServletRequest request, HttpServletResponse response) {
        exportFileUtil.setFileName("中间账户收入报表", "xlsx");
        // 构造数据导出
        try {
            int page = 1;
            int rows = 1000;
            exportFileUtil.initExport(response);
            request.setAttribute("pageExport", "yes");
            request.setAttribute("page", page);
            request.setAttribute("rows", rows);
            int begin_ = exportFileUtil.exportIOStream(response, selectIncomeListByquery(request, query), rows, page, 0);
            for (int i = 2; begin_ != page; i++) {
                page = begin_;
                request.setAttribute("page", i);
                begin_ = exportFileUtil.exportIOStream(response, selectIncomeListByquery(request, query), rows, begin_, 1);
            }
            exportFileUtil.finish(response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    /**
     * 查询数据
     *
     * @param query
     * @param request
     * @return
     */
    private List<MallMiddleIncomeAccount> selectIncomeListByquery(HttpServletRequest request, MallMiddleIncomeAccount query) {
        //获取开始时间范围
        String payStartTime = request.getParameter("payTime_begin");
        String payEndTime = request.getParameter("payTime_end");
        if (StringUtil.isNotEmpty(payStartTime)) {
            query.setPayStartTime(payStartTime);
        }
        if (StringUtil.isNotEmpty(payEndTime)) {
            query.setPayEndTime(payEndTime);
        }
        return reportService.selectMallMiddleIncomeAccount(query);

    }

    /*##########################中间商收入账户  end #########################*/



    /*##########################中间商支出账户  start #########################*/

    /**
     * 中间商支出账户列表
     */
    @RequestMapping(params = "middleExpenditureList")
    public ModelAndView middleExpenditureList() {
        return new ModelAndView("net/lahuobao/mall/finance/middleExpenditureList");
    }


    /**
     * 中间商收入账户 datagrid
     *
     * @param request
     * @param response
     */
    @RequestMapping(params = "middleExpenditureDatagrid")
    public void middleExpenditureDatagrid(MallMiddleExpenditureAccount query, HttpServletRequest request, HttpServletResponse response,
                                          DataGrid dataGrid) {
        //输出dataggrid
        TagUtil.datagrid(response, pageDateGrid(selectExpenditureListByquery(request, query), dataGrid));
    }


    @RequestMapping(params = "middleExpenditureExport")
    public void middleExpenditureExport(MallMiddleExpenditureAccount query, HttpServletRequest request, HttpServletResponse response) {
        exportFileUtil.setFileName("中间账户支出报表", "xlsx");
        // 构造数据导出
        try {
            int page = 1;
            int rows = 1000;
            exportFileUtil.initExport(response);
            request.setAttribute("pageExport", "yes");
            request.setAttribute("page", page);
            request.setAttribute("rows", rows);
            int begin_ = exportFileUtil.exportIOStream(response, selectExpenditureListByquery(request, query), rows, page, 0);
            for (int i = 2; begin_ != page; i++) {
                page = begin_;
                request.setAttribute("page", i);
                begin_ = exportFileUtil.exportIOStream(response, selectExpenditureListByquery(request, query), rows, begin_, 1);
            }
            exportFileUtil.finish(response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    /**
     * 查询数据
     *
     * @param query
     * @param request
     * @return
     */
    private List<MallMiddleExpenditureAccount> selectExpenditureListByquery(HttpServletRequest request, MallMiddleExpenditureAccount query) {
        //获取开始时间范围
        String payStartTime = request.getParameter("expenditureTime_begin");
        String payEndTime = request.getParameter("expenditureTime_end");
        if (StringUtil.isNotEmpty(payStartTime)) {
            query.setPayStartTime(payStartTime);
        }
        if (StringUtil.isNotEmpty(payEndTime)) {
            query.setPayEndTime(payEndTime);
        }
        return reportService.selectMallExpenditureAccount(query);

    }

     /*##########################中间商支出账户  end #########################*/


    /*##########################承运商支出账户  start #########################*/

    /**
     * 中间商支出账户列表
     */
    @RequestMapping(params = "carriersExpenditureList")
    public ModelAndView carriersExpenditure() {
        return new ModelAndView("net/lahuobao/mall/finance/carriersExpenditureList");
    }


    /**
     * 中间商收入账户 datagrid
     *
     * @param request
     * @param response
     */
    @RequestMapping(params = "carriersExpenditureDatagrid")
    public void carriersExpenditureDatagrid(MallCarriersExpenditureAccount query, HttpServletRequest request, HttpServletResponse response,
                                            DataGrid dataGrid) {
        //输出dataggrid
        TagUtil.datagrid(response, pageDateGrid(selectCarriersListByquery(request, query), dataGrid));
    }


    @RequestMapping(params = "carriersExpenditureExport")
    public void middleExpenditureExport(MallCarriersExpenditureAccount query, HttpServletRequest request, HttpServletResponse response) {
        exportFileUtil.setFileName("华昌支出报表", "xlsx");
        // 构造数据导出
        try {
            int page = 1;
            int rows = 1000;
            exportFileUtil.initExport(response);
            request.setAttribute("pageExport", "yes");
            request.setAttribute("page", page);
            request.setAttribute("rows", rows);
            int begin_ = exportFileUtil.exportIOStream(response, selectCarriersListByquery(request, query), rows, page, 0);
            for (int i = 2; begin_ != page; i++) {
                page = begin_;
                request.setAttribute("page", i);
                begin_ = exportFileUtil.exportIOStream(response, selectCarriersListByquery(request, query), rows, begin_, 1);
            }
            exportFileUtil.finish(response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    /**
     * 查询数据
     *
     * @param query
     * @param request
     * @return
     */
    private List<MallCarriersExpenditureAccount> selectCarriersListByquery(HttpServletRequest request, MallCarriersExpenditureAccount query) {
        //获取开始时间范围
        String payStartTime = request.getParameter("expenditureTime_begin");
        String payEndTime = request.getParameter("expenditureTime_end");
        if (StringUtil.isNotEmpty(payStartTime)) {
            query.setPayStartTime(payStartTime);
        }
        if (StringUtil.isNotEmpty(payEndTime)) {
            query.setPayEndTime(payEndTime);
        }
        return reportService.selectCarriersExpenditureAccount(query);

    }

     /*##########################承运商支出账户  end #########################*/


     /*##########################订单报表  start #########################*/

    /**
     * 获取订单报表页面
     *
     * @return
     */
    @RequestMapping(params = "orderReportList")
    public ModelAndView orderReportview(MallOrderReportQueryModel queryModel) {
        return new ModelAndView("net/lahuobao/mall/finance/mallOrderReportList");
    }

    /**
     * 订单报表 datagrid
     *
     * @param request
     * @param response
     */
    @RequestMapping(params = "orderReportListDatagrid")
    public void orderReportListDatagrid(MallOrderReportQueryModel query, HttpServletRequest request,
                                        HttpServletResponse response, DataGrid dataGrid) {
        //输出dataggrid
        TagUtil.datagrid(response, pageDateGrid(selectOrderReportByquery(request, query), dataGrid));
    }


    /**
     * 查询数据
     *
     * @param query
     * @param request
     * @return
     */
    private List<MallOrderReportResultModel> selectOrderReportByquery(HttpServletRequest request, MallOrderReportQueryModel query) {

        //设置时间查询条件
        if (StringUtil.isNotEmpty(request.getParameter("orderCreateTime_begin")) &&
                StringUtil.isNotEmpty(request.getParameter("orderCreateTime_end"))) {
            query.setOrderCreateTimeStart(DateUtils.str2Date(request.getParameter("orderCreateTime_begin"), DateUtils.date_sdf));
            query.setOrderCreateTimeEnd(DateUtils.str2Date(request.getParameter("orderCreateTime_end"), DateUtils.date_sdf));
        }

        if (StringUtil.isNotEmpty(request.getParameter("orderFinishTime_begin")) &&
                StringUtil.isNotEmpty(request.getParameter("orderFinishTime_end"))) {
            query.setOrderFinishTimeStart(DateUtils.str2Date(request.getParameter("orderFinishTime_begin"), DateUtils.date_sdf));
            query.setOrderFinishTimeEnd(DateUtils.str2Date(request.getParameter("orderFinishTime_end"), DateUtils.date_sdf));
        }

        return reportService.selectMallOrderReport(query);
    }


    /**
     * 订单报表导出
     * @param query
     * @param request
     * @param response
     */
    @RequestMapping(params = "orderReportExport")
    public void orderReportExport(MallOrderReportQueryModel query, HttpServletRequest request, HttpServletResponse response) {
        exportFileUtil.setFileName("商城订单报表", "xlsx");
        // 构造数据导出
        try {
            int page = 1;
            int rows = 1000;
            exportFileUtil.initExport(response);
            request.setAttribute("pageExport", "yes");
            request.setAttribute("page", page);
            request.setAttribute("rows", rows);
            int begin_ = exportFileUtil.exportIOStream(response, selectOrderReportByquery(request, query), rows, page, 0);
            for (int i = 2; begin_ != page; i++) {
                page = begin_;
                request.setAttribute("page", i);
                begin_ = exportFileUtil.exportIOStream(response, selectOrderReportByquery(request, query), rows, begin_, 1);
            }
            exportFileUtil.finish(response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

     /*##########################订单报表  end #########################*/


}
